package com.lantosec.hmrp.core.util;


import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.lantosec.hmrp.modules.workflow.vo.CustomAttributeVO;
import org.apache.commons.lang3.text.StrSubstitutor;

import java.lang.reflect.Field;
import java.util.Map;

public class StringUtils {

    public static String objToStr(Object obj) {
        return obj == null ? null : obj.toString();
    }

    public static Long objToLong(Object obj) {
        return obj == null ? null : Long.parseLong(obj.toString());
    }

    public static Boolean objToBoolean(Object obj) {
        return obj == null ? null : Boolean.parseBoolean(obj.toString());
    }

    public static Integer objToInteger(Object obj) {
        return obj == null ? null : Integer.parseInt(obj.toString());
    }

    /**
     * 首字母大写
     *
     * @param str
     * @return
     */
    public static String upperCaseFirst(String str) {
        return (str != null && str.length() > 1) ? str.substring(0, 1).toUpperCase() + str.substring(1) : "";
    }

    /**
     * 首字母小写
     *
     * @param str
     * @return
     */
    public static String lowerCaseFirst(String str) {
        return (str != null && str.length() > 1) ? str.substring(0, 1).toLowerCase() + str.substring(1) : "";
    }

    /**
     * 下划线，转换为驼峰式
     *
     * @param str
     * @return
     */
    public static String underlineToCamelCase(String str) {
        return toCamelCase(str, "_");
    }

    /**
     * 字符串转换为驼峰式
     *
     * @param str
     * @param separator
     * @return
     */
    public static String toCamelCase(String str, String separator) {
        StringBuilder result = new StringBuilder();
        if (str != null && str.trim().length() > 0) {
            boolean flag = false;
            for (int i = 0; i < str.length(); i++) {
                char ch = str.charAt(i);
                if (separator.charAt(0) == ch) {
                    flag = true;
                } else {
                    if (flag) {
                        result.append(Character.toUpperCase(ch));
                        flag = false;
                    } else {
                        result.append(ch);
                    }
                }
            }
        }
        return result.toString();
    }

    /**
     * 保存xml文件前替换自定义属性的名称，防止部署失败
     * @param source 数据
     */
    public static String addCustomAttrMark(String source) {
        if(org.apache.commons.lang3.StringUtils.isBlank(source)){return source;}
        Field[] fields = CustomAttributeVO.class.getDeclaredFields();
        for (int i = 0; i < fields.length; i++) {
            String fieldName = fields[i].getName();
            source = source.replaceAll(fieldName, "flowable:" + fieldName);
        }
        return source;
    }
    /**
     * xml文件返回给前端编辑前替换自定义属性的名称，防止前端js解析失败
     * @param source 数据
     */
    public static String removeCustomAttrMark(String source) {
        if (org.apache.commons.lang3.StringUtils.isBlank(source)) {
            return source;
        }
        Field[] fields = CustomAttributeVO.class.getDeclaredFields();
        for (int i = 0; i < fields.length; i++) {
            String fieldName = fields[i].getName();
            source = source.replaceAll("flowable:" + fieldName, fieldName);
        }
        return source;
    }

    /**
     * 自定义替换文字模板
     * @param template 模版（需要替换的用 ${target}）
     * @param params   参数对象
     * @return         替换后的模板
     */
    public static String processTemplate(String template, JSONObject params) {
        if (template == null || params == null) {
            return template;
        }
        Map<String, Object> valuesMap = JSONObject.parseObject(params.toJSONString(), new TypeReference<Map<String, Object>>() {});
        return processTemplate(template, valuesMap);
    }
    /**
     * 自定义替换文字模板
     * @param template  模版（需要替换的用 ${target}）
     * @param valuesMap 参数map对象
     * @return          替换后的模板
     */
    public static String processTemplate(String template, Map<String, Object> valuesMap) {
        if (template == null || valuesMap == null) {
            return template;
        }
        StrSubstitutor sub = new StrSubstitutor(valuesMap);
        return sub.replace(template);
    }

    public static void main(String[] str) {
        System.out.println(toCamelCase("sys_admin", "_"));
        System.out.println(upperCaseFirst(toCamelCase("sys_admin", "_")));
    }

    public static String splitStrByChar(String string,String chars){
        String substring = string.substring(0, string.lastIndexOf(chars));
        String str2=string.substring(substring.length()+1, string.length());
        return str2;
    }
}
